<?php

namespace App\Models\Tools;

use App\Exceptions\ApiException;
use App\Models\Admin\Setting;

class Base
{
    static function resJson($res){
        throw new ApiException([
            'code'=>$res['code'],
            'data'=>$res['data'],
            'msg'=>$res['msg']
        ]);
    }

    static function is_phone($mobile){
        if(preg_match("/^1\d{10}$/", $mobile)){
            return true;
        }else{
            return false;
        }
    }

    static public function is_today($time){
        if(date('Y-m-d') == date('Y-m-d',$time)){
            return true;
        }else{
            return false;
        }
    }

    static function input_select($n,$nv,array $arr,string $id='') {
        if($id){
            $html= '<select class="form-control" name="'.$n.'" id="'.$id.'">';
        }else{
            $html= '<select class="form-control" name="'.$n.'">';
        }
        foreach($arr as $k=>$v){
            if($k==$nv){
                $html.='<option value="'.$k.'" selected = "selected">'.$v.'</option>';
            }else{
                $html.='<option value="'.$k.'">'.$v.'</option>';
            }
        }
        $html.= '</select>';
        return $html;
    }

    public static function dropDown($column,?string $value=null) {
        $dropDownList = array(
            'is_status'=> array(
                '1'=> '开启',
                '0'=> '关闭',
            ),
        );
        if ($value) {
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;
        }else{
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
        }
    }
}
